HasLogger   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 27
rs 10
c 0
b 0
f 0
wmc 4

2 Functions

Rating   Name   Duplication   Size   Complexity  
A log 0 7 2
A createLogger 0 15 2
1
/* eslint-disable sort-keys */
2
3
import dayjs from 'dayjs';
4
import merge from 'deepmerge';
5
import winston, { Logger } from 'winston';
6
7
export type LogTarget = 'console' | 'file';
8
9
export class HasLogger {
10
    public logger!: Logger;
11
12
    public createLogger(targets: LogTarget[], defaultMeta: Record<string, any>) {
13
        this.logger = winston.createLogger({
14
            level: 'info',
15
            format: winston.format.json(),
16
            defaultMeta,
17
            transports: [],
18
        });
19
20
        if (targets.includes('file')) {
21
            new winston.transports.File({ filename: `${__dirname}/codeboost.log` });
22
        }
23
24
        //if (targets.includes('console')) {
25
        this.logger.add(new winston.transports.Console({ format: winston.format.simple() }));
26
        //}
27
    }
28
29
    public log(message: string, meta: any[] = []) {
30
        if (!this.logger || this.logger.transports.length === 0) {
31
            return;
32
        }
33
34
        this.logger.info(message, ...[merge.all([{ _ts: dayjs().toISOString() }, ...meta])]);
35
    }
36
}
37